package com.api.workflow.web;

import weaver.common.util.string.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @author wanghan
 * @version 1.0
 * @Description 客户授信调整
 * @since 2025/4/15 9:50
 */
public class CreditAdjustmentsAction  extends BaseBean implements Action {


    @Override
    public String execute(RequestInfo requestInfo) {
        try {
            writeLog("start------------------------CreditAdjustmentsActionCreditAdjustmentsActionCreditAdjustmentsAction");
            //获得流程id
            String workflowid = requestInfo.getWorkflowid();
            //获取表id
            String requestid = requestInfo.getRequestid();
            writeLog("start------------------------CreditAdjustmentsAction" + requestid);
            RecordSet rs = new RecordSet();
            rs.execute("select tablename from workflow_base a,workflow_bill b where a.formid=b.id and a.id=" + workflowid);
            rs.next();
            //获取主表名
            String maintable = Util.null2String(rs.getString("tablename"));

            //根据表id和主表名查找主表的相关信息
            rs.executeQuery("select * from " + maintable + " where requestid=?", new Object[]{requestid});
            rs.next();
            String sxnd = Util.null2String(rs.getString("sxnd"));//授信年度
            String xyd = Util.null2String(rs.getString("xyd"));//信用段

            String khbm = Util.null2String(rs.getString("khbm"));//主客户编码
            String khyed = Util.null2String(rs.getString("khyed"));//客户原额度
            String yxzjpfedwy = Util.null2String(rs.getString("yxzjpfedwy"));//调整后额度
            double zkhed = Double.valueOf(yxzjpfedwy) - Double.valueOf(khyed);
            rs.executeQuery("select * from uf_khsxxx where khbm=? and sxnd=? and sxxyd=?",khbm,sxnd,xyd);
            double khsxed=0.00;
            String khbmRequsetid="";
            if(rs.next()) {
                 khsxed = Double.valueOf(Util.null2String(rs.getString("sxze")));
                khbmRequsetid=Util.null2String(rs.getString("sxxglc"));
            }
            if(StringUtil.isNullOrEmpty(khbmRequsetid)){
                khbmRequsetid=requestid;
            }else{
                khbmRequsetid=khbmRequsetid+","+requestid;
            }
            double xkhed=khsxed+zkhed;
            rs.executeUpdate("update uf_khsxxx set sxze=? ,sxxglc=? where  khbm=? and sxnd=? and sxxyd=?",xkhed,khbmRequsetid,khbm,sxnd,xyd);
            writeLog("修改数据成功！");
            rs.executeQuery("select * from uf_khsxxx where khbm=? and sxnd=? and sxxyd=?",khbm,sxnd,xyd);
            String dcedkhbm = Util.null2String(rs.getString("dcedkhbm"));//次客户编码
            String tcyed = Util.null2String(rs.getString("tcyed"));//次客户原额度
            String tcxed = Util.null2String(rs.getString("tcxed"));//次客户调整后额度

            double zkhed1 = Double.valueOf(tcxed) - Double.valueOf(tcyed);
            rs.executeQuery("select * from uf_khsxxx where khbm=? and sxnd=? and sxxyd=?",dcedkhbm,sxnd,xyd);
            double khsxed1=0.00;
            String khbmRequsetid1="";
            if(rs.next()) {
                khsxed1 = Double.valueOf(Util.null2String(rs.getString("sxze")));
                khbmRequsetid1=Util.null2String(rs.getString("sxxglc"));
            }
            if(StringUtil.isNullOrEmpty(khbmRequsetid1)){
                khbmRequsetid1=requestid;
            }else{
                khbmRequsetid1=khbmRequsetid1+","+requestid;
            }
            double xkhed1=khsxed1+zkhed1;
            rs.executeUpdate("update uf_khsxxx set sxze=? ,sxxglc=? where  khbm=? and sxnd=? and sxxyd=?",xkhed1,khbmRequsetid1,dcedkhbm,sxnd,xyd);
            writeLog("修改数据成功！");

            return Action.SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("接口出错,请联系管理员!"+e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
